#!/usr/bin/env perl
#
# test script for Txtid.pm
#

# {{{ use block

use strict;
use warnings;

use lib '/home/jiri/code/txtid/lib';

use Data::Dumper;
use utf8;
use Test::More;
use Txtid             qw(:ALL);

# }}}
# {{{ variables declaration

my $test_num;

# }}}
# {{{ testing

require_ok( 'Txtid' );
$test_num++;

# get_histogram

my $data_struct = {A => 2, B => 1, C => 1, D => 2, E => 3 };
my $expected    = {1 => 2, 2 => 2, 3 => 1};
my $got         = get_histogram($data_struct);
is_deeply($got, $expected, 'get_histogram: simply histogram');
$test_num++;

# get_tag

my $file  = 'pokus';
my @log   = ('word_order');
$expected = ['o'];
$got      = get_tag(@log);
is_deeply($got, $expected, 'get_tag: create tag');
$test_num++;

# extract_ngrams

$data_struct = ['A','B','C','D'];
$expected    = {'A B' => 1, 'B C' => 1, 'C D' => 1};
$got         = extract_ngrams($data_struct, 2);
is_deeply($got, $expected, 'extract_ngrams: get bigrams');
$test_num++;

$expected    = {'A B C' => 1, 'B C D' => 1};
$got         = extract_ngrams($data_struct, 3);
is_deeply($got, $expected, 'extract_ngrams: get trigrams');
$test_num++;

$expected    = {'A' => 1, 'B' => 1, 'C' => 1, 'D' => 1};
$got         = extract_ngrams($data_struct, 1);
is_deeply($got, $expected, 'extract_ngrams: get unigrams');
$test_num++;

# modify_words

$expected = [[qw(V C C C)]];
$got      = modify_words([$data_struct]);
is_deeply($got, $expected, 'modify_words: show phonological structure');
$test_num++;

# tokenize

$data_struct = 'Máma šla do lesa';
$expected    = [qw(Máma šla do lesa)];
$got         = tokenize($data_struct);
is_deeply($got, $expected, 'tokenize: tokenize simply sentence');
$test_num++;

# compare distributions

my $orig  = [qw(máma máma táta táta kiki modroočko)];
my $mod   = [qw(máma máma táta kiki kiki modroočko)];
$expected = 0;
$got      = compare_distributions($orig, $orig);
is($got, $expected, 'compare_ditributions: identical distributions -> distance must be 0');
$test_num++;

# compare_rhythm

$orig     = [[qw(V C N N P P)]];
$mod      = [[qw(V N N N P P)]];
$expected = 1;
$got      = compare_rhythm($orig,$mod);
is($got, $expected, 'compare_rhythm: almost similar sequences');
$test_num++;

# compare_tokens_frequencies

$orig     = [qw(V C N N P P)];
$expected = 1;
$got      = compare_tokens_frequencies($orig,$orig);
is($got, $expected, 'compare_tokens_frequencies: correlation between identical datasets must be 1');
$test_num++;

# count_ttr

$orig     = [qw(máma máma táta táta kiki modroočko)];
$mod      = [qw(máma máma táta kiki kiki modroočko)];
$expected = [ (4 / 6),(4 / 6) ];
$got      = count_ttr($orig, $mod);
is_deeply($got, $expected, 'count_ttr: count types/tokens ratio');
$test_num++;

$orig     = [qw(máma máma táta táta kiki modroočko x x y y y y)];
$mod      = [qw(máma máma kiki kiki kiki modroočko m m c c c c)];
$expected = [ ( 6 / 12),(5 / 12) ];
$got      = count_ttr($orig, $mod);
is_deeply($got, $expected, 'count_ttr: different ttr');
$test_num++;

$orig = {
    'máma a'    => 5,
    'máma ,'    => 5,
    'mámě dal'  => 5,
    'mámě dala' => 5,
};

$mod = {
    'máma a'   => 5,
    'máma ,'   => 5,
    'máma dát' => 10,
};

$expected = [ (4 / 20) , ( 3 / 20 )];
$got      = count_ttr($orig, $mod);
is_deeply($got, $expected, 'count_ttr: hash input');
$test_num++;

# get_sum

$orig     = {A => 1, B => 2, c => 3};
$expected = 6;
$got      = Txtid::get_sum($orig);
is($got, $expected, 'get_sum: basic sum');
$test_num++;

# euclidean distance

$orig     = {A => 3, B => 3, C => 3};
$expected = 0;
$got      = euclidean_distance($orig,$orig);
is($got, $expected, 'euclidean distance: identical data structs must have distance 0');
$test_num++;

$orig     = {A => 3, B => 3, C => 3};
$mod      = {A => 4, B => 4, C => 4};
$expected = sqrt 3;
$got      = euclidean_distance($orig, $mod);
is($got, $expected, 'euclidean distance: non identical data structs');
$test_num++;

# levenshtein

$orig     = 'agata';       # hanychova :-)
$mod      = 'spagata';     # snuptychlova :-D
$expected = 2;
$got      = levenshtein_distance($orig, $mod);
is($got, $expected, 'levenshtein distance: agata vs. spagata = 2');
$test_num++;

# pearson correlation

$orig     = [[1,1],[2,2,],[3,3]];
$expected = 1;
$got      = pearson_correlation($orig);
is($got, $expected, 'Pearson correlation: absolute positive correlation');
$test_num++;

$orig     = [[1,3],[2,2,],[3,1]];
$expected = - 1;
$got      = pearson_correlation($orig);
is($got, $expected, 'Pearson correlation: absolute negative correlation');
$test_num++;

# flatten

$orig     = ['A',['B',1],'C',[2],[3],];
$expected = ['A','B',1,'C',2,3,];
$got      = flatten($orig);
is_deeply($got, $expected, 'flatten: AoA');
$test_num++;

# make vector

$orig     = {A => 1, B => 2, C => 3, D => 4};
$mod      = {A => 1, B => 4, D => 3};
$expected = [[1,1],[2,4,],[3,0],[4,3]];
$got      = make_vector($orig, $mod);
is_deeply($got, $expected, 'make_vector: simply vector creation');
$test_num++;

# transform actions

$orig     = ['AA,BB,CC,'];
$expected = {AA => 1, BB => 1, CC => 1};
$got      = transform_actions($orig);
is_deeply($got, $expected, 'transform_actions: input options');
$test_num++;

# transform l2h

$orig     = [qw(A A B B C C C D)];
$expected = { A => 2, B => 2, C => 3, D => 1};
$got      = transform_l2h($orig);
is_deeply($got, $expected, 'transform_l2h: arr to hash conversion');
$test_num++;

done_testing($test_num);

# }}}




